from tornado.web import RequestHandler

from core.decorators.open_handler import open_handler
from core.decorators.open_api.index import BaseRequestHandler, openApiFactory
from core.utils.exceptions import NeedRenderException


@open_handler(path="/api/(.*)", index=1)
class openApiHandlerService(BaseRequestHandler):

    # 在这里复写父类的excute函数便可
    def execute(self, args, params, context):
        # 获取到相应的处理函数
        api_name = args[0]
        _method = self.request.method
        _accept = self.request.headers.get("accept")

        def get_interface_documentation():
            # 获取接口文档
            pass

        # 判断是否返回接口文档还是json数据
        if _method == "GET" and _accept and "text/html" in _accept:
            # 通过抛出不同的错误来决定返回的数据类型,渲染返回的类型的时候，我们就要在要错误抓取时
            raise NeedRenderException("index.html",{})
        else:
            result = openApiFactory.execute_open_api(api_name, params)
            return result
